package fl.ass.service.api;

import java.util.List;

import cmm.comm.facade.framework.api.ISysRegionApiService;
import cmm.comm.facade.framework.condition.SysRegionQueryCondition;
import cmm.comm.facade.framework.dto.SysRegionDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import fl.ass.facade.api.IAssTrailCarBorrowApi;
import fl.ass.facade.condition.AssTrailCarBorrowQueryCondition;
import fl.ass.facade.condition.AssTrailCarDInfQueryCondition;
import fl.ass.service.dao.AssTrailCarBorrowMapper;
import fl.ass.service.dao.AssTrailCarDInfMapper;
import fl.ass.service.dao.AssTrailCarMMapper;
import fl.ass.facade.dto.AssTrailCarBorrowDTO;
import fl.ass.facade.dto.AssTrailCarDInfDTO;
import fl.ass.facade.dto.AssTrailCarMDTO;
import fl.ass.service.po.AssTrailCarBorrow;
import fl.ass.service.po.AssTrailCarDInf;
import fl.ass.service.transform.AssTrailCarBorrowTransform;
import fl.ass.service.transform.AssTrailCarDInfTransform;
import fl.ass.service.transform.AssTrailCarMTransform;
@Service
public class AssTrailCarBorrowApiImpl implements IAssTrailCarBorrowApi {
	
	private static final Logger logger = LoggerFactory.getLogger(AssTrailCarBorrowApiImpl.class);

	@Autowired
	private AssTrailCarBorrowMapper assTrailCarBorrowMapper;
	
	
	@Autowired
	private AssTrailCarMMapper  assTrailCarMMapper;
	
	@Autowired
	private AssTrailCarDInfMapper assTrailCarDInfMapper;

	@Autowired
    private ISysRegionApiService sysRegionApiService;

	@Override
	public int updateAssTrailCarBorrow(AssTrailCarBorrowDTO dto) {
		AssTrailCarBorrow po = AssTrailCarBorrowTransform.toPO(dto);
		return assTrailCarBorrowMapper.update(po);
	}

	@Override
	public AssTrailCarBorrowDTO searchAssTrailCarBorrow(Long applyId) {
		if(applyId == null){
			return null;
		}
		AssTrailCarBorrowQueryCondition  condition = new AssTrailCarBorrowQueryCondition();
		condition.setIsDel(0L);
		condition.setApplyId(applyId);
		List<AssTrailCarBorrow> selectList = assTrailCarBorrowMapper.selectList(condition);
		if(CollectionUtils.isEmpty(selectList)){
			return null;
		}
		return AssTrailCarBorrowTransform.toDTO(selectList.get(0));
	}

	@Override
	public AssTrailCarMDTO selectAssTrailCarMDTOById(Long id) {
        AssTrailCarMDTO assTrailCarMDTO = AssTrailCarMTransform.toDTO(assTrailCarMMapper.selectById(id));
        if (null != assTrailCarMDTO) {
            /*
              获取省市信息开始
             */
            StringBuffer provinceCity = new StringBuffer();
            SysRegionQueryCondition qc = new SysRegionQueryCondition();
            qc.setSysCd("11000");
            qc.setRgnLvl(1);
            qc.setRgnCd(assTrailCarMDTO.getProvinceCd());
            List<SysRegionDTO> sysRegionProvince = this.sysRegionApiService.searchRegionList(qc);
            if (CollectionUtils.isNotEmpty(sysRegionProvince)) {
                provinceCity.append(sysRegionProvince.get(0).getRgnNm() + "-");
            }
            qc.setRgnLvl(2);
            qc.setRgnCd(assTrailCarMDTO.getCityCd());
            List<SysRegionDTO> sysRegionCity = this.sysRegionApiService.searchRegionList(qc);
            if (CollectionUtils.isNotEmpty(sysRegionCity)) {
                provinceCity.append(sysRegionCity.get(0).getRgnNm());
            }
            assTrailCarMDTO.setProvinceCity(provinceCity.toString());
            /*
              获取省市信息结束
             */
        }
        return assTrailCarMDTO;
	}

	@Override
	public int updateAssTrailCarMDTO(AssTrailCarMDTO dto) {
		int result = -1;
		try {
			result = assTrailCarMMapper.update(AssTrailCarMTransform.toPO(dto));
		} catch (Exception e) {
			BaseLogUtils.newLogger("拖车流程放款修改主表错误").setThrowable(e).info();
			throw e;
		}
		return result;
	}

	@Override
	public AssTrailCarDInfDTO selectAssTrailCarDInfDTOById(Long Mid) {
		// TODO Auto-generated method stub
		AssTrailCarDInfQueryCondition condition = new AssTrailCarDInfQueryCondition();
		condition.setMasterId(Mid);
		List<AssTrailCarDInf> selectList = assTrailCarDInfMapper.selectList(condition);
		if(CollectionUtils.isEmpty(selectList)){
			return null;
		}
		AssTrailCarDInfDTO dto = AssTrailCarDInfTransform.toDTO(selectList.get(0));
		if(dto.getMasterId().equals(Mid)){
			return dto;
		}
		return null;
	}

	@Override
	public int updateAssTrailCarDInfDTO(AssTrailCarDInfDTO dto) {
		int result = -1;
		try {
			result = assTrailCarDInfMapper.update(AssTrailCarDInfTransform.toPO(dto));
		} catch (Exception e) {
		    BaseLogUtils.newLogger("拖车流程放款修改拖车实际明细表错误").setThrowable(e).info();
		    throw e;
		}
		return result;
	}
	
	

}
